﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Data;
using HugeFlow.Utility;
using HugeFlow.CommandPattern.Commands;
using System.ComponentModel;

namespace HugeFlow.CommandPattern.Interactivity
{ 
    public abstract class ExecuteCommandBehaviorBase : Behavior<FrameworkElement>
    {
        #region Properties

        [Category("Command")]
        public Binding CommandParameter { get; set; }

        public abstract ICommand CommandTriggers { get; }

        #endregion Properties

        #region Methods

        protected Binding GetProperBinding(FrameworkElement target, Binding binding)
        {
            if (binding.ElementName != null)
            {
                FrameworkElement fe = AssociatedObject.FindName(binding.ElementName) as FrameworkElement;
                if (fe != null)
                {
                    binding.ElementName = null;
                    Binding bind2 = binding.Clone<Binding>();
                    bind2.Source = fe;
                    binding = bind2;
                }
            }
            else
            {
                target.SetBinding(FrameworkElement.DataContextProperty, new Binding("DataContext") { Source = AssociatedObject });
            }

            return binding;
        }

        #endregion Methods
    }
}
